home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 151-175 / scopedisk168 / serial / serial.c < prev    next >
C/C++ Source or Header  |  1995-03-19  |  12KB  |  382 lines

  1. /* SERIAL.C */
  2.  
  3. #include        <exec/types.h>
  4. #include        <devices/serial.h>
  5. #undef NULL
  6. #include        <stdio.h>
  7.  
  8. extern struct Port *CreatePort();
  9. struct Port *mySerWritePort;
  10. struct IOExtSer *mySerWriteReq;
  11.  
  12. /***********************************************************************/
  13. main( argc, argv ) 
  14.    int argc;
  15.    char *argv[];
  16. {
  17.    char program[30], qline[5] ;
  18.    int i, iunit, delay, unit[9], qunit, qvalue, loop ;
  19.  
  20.    loop = - 1 ;
  21.    delay = 50 ;
  22.    qunit = -1 ;
  23.    strcpy( qline, "XX" ) ;
  24.    for (iunit=0;iunit<9;iunit++) {
  25.       unit[iunit] = 0 ;
  26.    }
  27.  
  28. /*
  29.    printf( "argc is %d \n", argc ) ;
  30.    for (i=0;i<argc;++i) {
  31.       printf( "arg # %d is %s \n", i, argv[i] ) ;
  32.    }
  33.    printf( "%s \n", *argv ) ;
  34. */
  35.      
  36.    i = option( argc, argv, unit, &delay, program, qline, &qunit, &qvalue,
  37.        &loop ) ;
  38. /*
  39.    printf( "qline is  %s \n", qline ) ;  
  40.    printf( "qunit is  %d \n", qunit ) ;
  41.    printf( "qvalue is %d \n", qvalue ) ;
  42.    printf( "loop   is %d \n", loop ) ;
  43.    printf( "Program is: %s \n", program ) ;
  44.    for (iunit=0;iunit<9;iunit++) {
  45.       printf("Unit %d is %d \n", iunit, unit[iunit] ) ;
  46.    }
  47.    printf( "Delay is %d \n", delay ) ;
  48. */
  49.    if( i == 0 ) {
  50.       OpenSerial( unit, delay, program, qline, qunit, qvalue, loop );
  51.    }
  52.    else {
  53.       printf( "No execution due to above error \n" ) ;
  54.    }   
  55.    return ( 0 ) ;
  56. }  /* main() */
  57. /**********************************************************************/
  58. option( argc, argv, unit, pdelay, program, qline, pqunit, pqvalue, ploop )
  59.    int argc, *pdelay, *pqunit, *pqvalue, *ploop, unit[] ;
  60.    char program[], qline[] ;
  61.    char *argv[] ;
  62. {
  63.    int i ;
  64.  
  65.    i = 1 ;
  66.    while( i < argc ) {
  67.       if( strcmpu( argv[i], "?" ) == 0 ) {
  68.          help( ) ;
  69.          return( 1 ) ;
  70.       }
  71.       if( i == 1 ) {
  72.          strcpy( program, argv[i] ) ;
  73.       }
  74.       else if( strcmpu( argv[i], "L" ) == 0 ) {
  75.          i = i + 1 ;
  76.          sscanf( argv[i], "%d", ploop ) ;
  77.       }
  78.       else if( strcmpu( argv[i], "U" ) == 0 ) {
  79.          i = i + 1 ;
  80.          if(      strcmpu( argv[i], "0" ) == 0 ) unit[0] = 1 ;
  81.          else if( strcmpu( argv[i], "1" ) == 0 ) unit[1] = 1 ;
  82.          else if( strcmpu( argv[i], "2" ) == 0 ) unit[2] = 1 ;
  83.          else if( strcmpu( argv[i], "3" ) == 0 ) unit[3] = 1 ;
  84.          else if( strcmpu( argv[i], "4" ) == 0 ) unit[4] = 1 ;
  85.          else if( strcmpu( argv[i], "5" ) == 0 ) unit[5] = 1 ;
  86.          else if( strcmpu( argv[i], "6" ) == 0 ) unit[6] = 1 ;
  87.          else if( strcmpu( argv[i], "7" ) == 0 ) unit[7] = 1 ;
  88.          else if( strcmpu( argv[i], "8" ) == 0 ) unit[8] = 1 ;
  89.          else {
  90.             printf( "Illegal unit is %s \n", argv[i] ) ;
  91.             return( 1 ) ;
  92.          }
  93.       }
  94.       else if( strcmpu( argv[i], "D" ) == 0 ) {
  95.          i = i + 1 ;
  96.          sscanf( argv[i], "%d", pdelay ) ;
  97.       }
  98.       else if( strcmpu( argv[i], "Q" ) == 0 ) {
  99.          /* get the quit option:   Q line unit value  */
  100.          i = i + 1 ;
  101.          if( strcmpu( argv[i], "CD" ) == 0 ) {
  102.             strcpy( qline, "CD" ) ;
  103.          }
  104.          else if( strcmpu( argv[i], "CTS" ) == 0 ) {
  105.             strcpy( qline, "CTS" ) ;
  106.          }
  107.          else if( strcmpu( argv[i], "DSR" ) == 0 ) {
  108.             strcpy( qline, "DSR" ) ;
  109.          }
  110.          else {
  111.             printf( "Illegal line value was %s, expecting CD, CTS, or DSR\n",
  112.             argv[i] ) ;
  113.             return( 1 ) ;
  114.          }
  115.          i = i + 1 ;
  116.          if(      strcmpu( argv[i], "0" ) == 0 ) *pqunit = 0 ;
  117.          else if( strcmpu( argv[i], "1" ) == 0 ) *pqunit = 1 ;
  118.          else if( strcmpu( argv[i], "2" ) == 0 ) *pqunit = 2 ;
  119.          else if( strcmpu( argv[i], "3" ) == 0 ) *pqunit = 3 ;
  120.          else if( strcmpu( argv[i], "4" ) == 0 ) *pqunit = 4 ;
  121.          else if( strcmpu( argv[i], "5" ) == 0 ) *pqunit = 5 ;
  122.          else if( strcmpu( argv[i], "6" ) == 0 ) *pqunit = 6 ;
  123.          else if( strcmpu( argv[i], "7" ) == 0 ) *pqunit = 7 ;
  124.          else if( strcmpu( argv[i], "8" ) == 0 ) *pqunit = 8 ;
  125.          else {
  126.             printf( "Illegal quit unit is %s, expecting 0 thru 8 \n", 
  127.             argv[i] ) ;
  128.             return( 1 ) ;
  129.          }
  130.          i = i + 1 ;
  131.          if(      strcmpu( argv[i], "0" ) == 0 ) *pqvalue = 0 ;
  132.          else if( strcmpu( argv[i], "1" ) == 0 ) *pqvalue = 1 ;
  133.          else {
  134.             printf( "Illegal quit value is %s, expecting 0 or 1 \n", 
  135.             argv[i] ) ;
  136.             return( 1 ) ;
  137.          }
  138.       }
  139.       else {
  140.          printf( "Illegal option %s \n", argv[i] ) ;
  141.          return( 1 ) ;
  142.       }
  143.       i = i + 1 ;
  144.    }
  145.    return( 0 ) ;
  146. }
  147. /**********************************************************************/
  148. OpenSerial( unit, delay, program, qline, qunit, qvalue, loop )
  149.    char program[], qline[] ;
  150.    int unit[], delay, qunit, qvalue, loop ;
  151. {
  152.  
  153.    /* create a reply port to which serial device can return the request */
  154.    mySerWritePort = CreatePort( "mySerial", 0 ) ;
  155.    if( mySerWritePort == NULL ) {
  156.       printf( "Problems during CreatePort \n" ) ;   /* can't create port */
  157.       }
  158.    else {
  159.       /* create a request block appropriate to serial */
  160.       mySerWriteReq = (struct IOExtSer *)CreateExtIO(mySerWritePort,
  161.                sizeof( struct IOExtSer ) ) ;
  162.       if( mySerWriteReq == NULL ) {
  163.          printf( "CreateExtIO error \n" ) ; /* error during CreateExtIO! */
  164.       }
  165.       else {
  166.          mySerWriteReq->io_SerFlags = 0 ;
  167.  
  168.          looper( unit, delay, program, qline, qunit, qvalue, loop ) ;
  169.  
  170.          /*     printf( "Closing IOExtSer... \n" ) ; */
  171.          /* delete request block */
  172.          DeleteExtIO( mySerWriteReq, sizeof(struct IOExtSer) ) ;
  173.       }
  174.       /* printf ("Closing Port...\n");  */
  175.       DeletePort( mySerWritePort ) ;
  176.    }
  177.    return ( 0 ) ;
  178. }  
  179. /************************************************************************/
  180. int looper( unit, delay, program, qline, qunit, qvalue, loop )
  181.    char program[], qline[] ;
  182.    int unit[], delay, qunit, qvalue, loop ;
  183. {
  184.    char cmd[255] ;
  185.    int cont, error, iunit, loopc, qloop ;
  186.    int cd[9], cdold[9], cts[9], ctsold[9], dsr[9], dsrold[9] ;
  187.  
  188.    for( iunit=0;iunit<9;iunit++) {
  189.       cdold[iunit]  = -1 ;
  190.       ctsold[iunit] = -1 ;
  191.       dsrold[iunit] = -1 ;
  192.    }
  193.    qloop = loop ;
  194.    loopc = 0 ;
  195.    cont = 1 ;
  196.    if( loop < 0 && qunit < 0 ) {
  197.       /* no quiting constraints.  So lets do this once. */
  198.       qloop = 1 ;
  199.    }
  200.    while( cont == 1 ) {
  201. /*      printf( "At top of loop \n" ) ; */
  202.       for( iunit=0;iunit<9;iunit++) {
  203.          if( unit[iunit] == 1 ) {
  204.             error = OpenDevice( "serial.device", iunit, mySerWriteReq, 0 ) ;
  205.             if( error != 0 ) {
  206.                printf( "Serial device unit %d did not open \n", iunit ) ; 
  207.                /* device not available */
  208.             }
  209.             else {
  210.                WriteSer( mySerWriteReq, iunit, dsr, cts, cd, "test", 4 ) ;
  211.                /* print_request( mySerWriteReq ); */
  212.                CloseDevice( mySerWriteReq ) ;
  213.                if( cd[iunit] != cdold[iunit] ) {
  214.                   cdold[iunit] = cd[iunit] ;
  215.                   cmdup( cmd, program, "CD", iunit, cd[iunit] ) ; 
  216.                   if( qunit >= 0 ) {
  217.                      if( strcmp( qline, "CD" ) == 0 ) {
  218.                         if( qvalue == cd[qunit] ) {
  219.                            cont = 0 ;
  220.                         }
  221.                      }
  222.                   }
  223.                }
  224.                if( cts[iunit] != ctsold[iunit] ) {
  225.                   ctsold[iunit] = cts[iunit] ;
  226.                   cmdup( cmd, program, "CTS", iunit, cts[iunit] ) ; 
  227.                   if( qunit >= 0 ) {
  228.                      if( strcmp( qline, "CTS" ) == 0 ) {
  229.                         if( qvalue == cts[qunit] ) {
  230.                            cont = 0 ;
  231.                         }
  232.                      }
  233.                   }
  234.                }
  235.                if( dsr[iunit] != dsrold[iunit] ) {
  236.                   dsrold[iunit] = dsr[iunit] ;
  237.                   cmdup( cmd, program, "DSR", iunit, dsr[iunit] ) ; 
  238.                   if( qunit >= 0 ) {
  239.                      if( strcmp( qline, "DSR" ) == 0 ) {
  240.                         if( qvalue == dsr[qunit] ) {
  241.                            cont = 0 ;
  242.                         }
  243.                      }
  244.                   }
  245.                }
  246.             }
  247.          }
  248.       }
  249. /*      if( strcmp( cmd, "" ) != 0 ) {
  250.          printf( "%s \n", cmd ) ;
  251.          Execute( cmd, 0, 0 ) ;
  252.       }
  253. */
  254.       if( cont == 0 ) {
  255.          printf( "Quiting because of condition on unit %d line %s \n",
  256.          qunit, qline ) ;
  257.       }
  258.       if( qloop > 0 ) {
  259.          loopc = loopc + 1 ;
  260.          if( loopc >= qloop ) {
  261.             cont = 0 ;
  262.             printf( "Quiting because loop count of %d was reached \n",
  263.             qloop ) ;
  264.          }
  265.       }
  266.       if( cont == 1 ) {
  267.          Delay( delay ) ;
  268.       }
  269.    }
  270.    return( 0 ) ;
  271. }
  272. /************************************************************************/
  273. int cmdup( cmd, program, bit, iunit, status ) 
  274.    char cmd[], bit[], program[] ;
  275.    int iunit, status ;
  276. {
  277.    char string[10] ;
  278.    int value ;
  279.  
  280.    strcpy( cmd, "rx " ) ;
  281.    strcat( cmd, program ) ;
  282.    strcat( cmd, ".rexx SERIAL " ) ;
  283.    strcat( cmd, bit ) ;
  284.    value = status ;
  285.    if( value != 0 ) {
  286.       value = 1 ;
  287.    }
  288.    sprintf( string, " %d %d ", iunit, value ) ;
  289.    strcat( cmd, string ) ;
  290.    Execute( cmd, 0, 0 ) ;
  291.    return( 0 ) ;
  292. }
  293. /************************************************************************/
  294. int WriteSer( io, iunit, dsr, cts, cd, data, length )
  295.    struct IOExtSer *io ;
  296.    char *data ;
  297.    int cd[], cts[], dsr[], length, iunit ;
  298. {
  299.    int error;
  300.  
  301.    io->IOSer.io_Data    = (APTR)data;
  302.    io->IOSer.io_Length  = length ;
  303.    io->IOSer.io_Command = CMD_WRITE ;
  304.    io->IOSer.io_Command = SDCMD_QUERY ;
  305.    error = DoIO( io ) ;
  306.    if( error != 0 ) {
  307.       printf( "serial.device write error \n" ) ;
  308.       }
  309.    else {
  310.       /* look for Request To Send, Data Set Ready, and Carrier Detect */
  311. /*
  312.       printf( "io_Status is %d \n", io->io_Status ) ;
  313. */
  314.       dsr[iunit] = io->io_Status & 010 ;
  315.       cts[iunit] = io->io_Status & 020 ;
  316.       cd[iunit]  = io->io_Status & 040 ;
  317.       if( dsr[iunit] != 0 ) {
  318.          dsr[iunit] = 1 ;
  319.       }
  320.       if( cd[iunit] != 0 ) {
  321.          cd[iunit] = 1 ;
  322.       }
  323.       if( cts[iunit] != 0 ) {
  324.          cts[iunit] = 1 ;
  325.       }
  326. /*
  327.       printf( "For unit %d \n", iunit ) ;
  328.       printf( "  DSR is %d \n", dsr[iunit] ) ;
  329.       printf( "  CTS is %d \n", cts[iunit] ) ;
  330.       printf( "  CD  is %d \n", cd[iunit]  ) ;
  331. */
  332.    }
  333.    return( error ) ;
  334. }
  335. /************************************************************************/
  336. print_request( IORser )
  337.     struct IOExtSer *IORser;     /* Serial port IO request block */
  338. {
  339. #define PRINT( field )           printf( "      %s = %8lx %8ld\n" \
  340.                                  , "field", (ULONG) IORser->field \
  341.                                  , (ULONG) IORser->field )
  342. #define IPRINT( index, field ) printf( " %2ld   %s = %8lx %8ld\n" \
  343.                                     , index, "field", (ULONG) IORser->field \
  344.                                                     , (ULONG) IORser->field )
  345.  
  346.       printf( "index field name       hexadec  decimal\n" ); 
  347.       IPRINT( 1, io_CtlChar   ); 
  348.       IPRINT( 2, io_RBufLen   );
  349.       IPRINT( 3, io_ExtFlags  ); 
  350.       IPRINT( 4, io_Baud      );
  351.       IPRINT( 5, io_BrkTime   ); 
  352.       IPRINT( 6, io_TermArray.TermArray0   );
  353.       IPRINT( 7, io_TermArray.TermArray1   );
  354.       IPRINT( 8, io_ReadLen   );
  355.       IPRINT( 9, io_WriteLen  );
  356.       IPRINT(10, io_StopBits  );
  357.       IPRINT(11, io_SerFlags  );
  358.       printf( "\n" );   /* Not associated with an index */
  359.           PRINT( io_Status    );
  360. /*
  361.       UWORD  io_Status
  362.       status of serial port, as follows:
  363.  
  364.       BIT   ACTIVE  FUNCTION
  365.         0      low  busy
  366.         1      low  paper out
  367.         2      low  select
  368.         3      low  Data Set Ready
  369.         4      low  Clear To Send
  370.         5      low  Carrier Detect
  371.         6      low  Ready To Send
  372.         7      low  Data Terminal Ready
  373.         8     high  read overrun
  374.         9     high  break event
  375.        10     high  break recieved
  376.        11     high  transmit x-OFFed
  377.        12     high  recieve x-OFFed
  378.     13-15    (not)  reserved
  379. */
  380.       return ( 0 ) ;
  381. }  /* print_request() */
  382.